package com.yuyaogc.lowcode.engine.container;

import com.yuyaogc.lowcode.engine.entity.Module;
import com.yuyaogc.lowcode.engine.entity.Entity;
import com.yuyaogc.lowcode.engine.entity.EntityClass;

import java.util.*;

/**
 * 容器
 */
public class Container {

    private static Container container;

    public static Container me() {
        if (container == null) {
            container = new Container();
        }
        return container;
    }

    private final Map<String, Module> modules = new HashMap<>();
    private final Map<String, Entity> metas = new LinkedHashMap<>();



    public Container() {
    }






    public void putEntity(Entity entity){
        metas.put(entity.getId(), entity);
    }

    public Collection<Module> getApps() {
        return modules.values();
    }



    public Module get(String name) {
        return modules.get(name);
    }

    public void add(Module module) {
        modules.put(module.getName(), module);
    }

    public void remove(String name) {
        Module application =  get(name);
        if(!Objects.isNull(application)){
            application.onDestroy();
        }
        modules.remove(name);
    }




    public void clear(){
        modules.clear();
    }

    public EntityClass getEntityClass(String className) {
        for (Module app : modules.values()) {
            if (app.containsKey(className)) {
                return app.getEntity(className);
            }
        }
        return null;
    }



    public void buildInherit(){
        for(Module application: getApps()){
            application.buildInherit();
        }
    }

}
